package com.book.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.book.dao.BookDao;
import com.book.domain.Book;

/**
 * Servlet implementation class AddBookServlet
 */
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddBookServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("deprecation")
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		long sizeMax = 30 * 1024 * 1024;
		String bookimgurl = "";
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		diskFileItemFactory.setSizeThreshold(4096);
		diskFileItemFactory.setRepository(new File(request.getRealPath("/")
				+ "upload"));// 设置上传文件目录
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				diskFileItemFactory);
		servletFileUpload.setSizeMax(sizeMax);
		List<?> fileitemList = null;
		// Book book=new Book();
		try {
			fileitemList = servletFileUpload.parseRequest(request);
			Iterator<?> iterator = fileitemList.iterator();
			String path = "";
			while (iterator.hasNext()) {
				FileItem fileItem = (FileItem) iterator.next();
				if (fileItem.isFormField()) {
					request.setAttribute(fileItem.getFieldName(),
							fileItem.getString("utf-8"));
				} else {
					path = fileItem.getName();
					String filename = path
							.substring(path.lastIndexOf("\\") + 1);
					String extname = filename.substring(filename
							.lastIndexOf(".") + 1);
					long now = System.currentTimeMillis();
					String prefix = String.valueOf(now);
					String newname = request.getRealPath("/") + "upload/"
							+ prefix + "." + extname;
					System.out.println(newname);
					bookimgurl = prefix + "." + extname;
					try {
						fileItem.write(new File(newname));
						System.out.println("上传文件写入成功");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		String bookname = (String) request.getAttribute("bookname");
		String authorname = (String) request.getAttribute("authorname");
		String publishername = (String) request.getAttribute("publishername");
		String bookcatalog = (String) request.getAttribute("bookcatalog");
		String bookoutline = (String) request.getAttribute("bookoutline");
		Book book = new Book(0, bookname, authorname, publishername,
				bookcatalog, bookimgurl, bookoutline);
		BookDao bookDao = new BookDao();
		bookDao.addBook(book);
		System.out.println(bookname);
		response.getWriter().write("添加图书成功");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
